package net.gianrishandy.jbase.component;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.gianrishandy.jbase.component.adapter.DataListAdapter;
import net.gianrishandy.jbase.component.adapter.LookupAdapter;
import net.gianrishandy.jbase.component.event.DataListEvent;
import net.gianrishandy.jbase.component.event.LookupEvent;
import net.gianrishandy.jbase.component.listener.LookupListener;
import net.gianrishandy.jbase.model.DataTable;
import javax.swing.ImageIcon;

/**
 * @author Gian Rishandy
 *
 */
public class LookupField extends JPanel {

	private static final long serialVersionUID = 312926175247206961L;
	private DataList dataList;
	private DataTable dataTable;
	private JButton button;
	private JTextField textField;

	/**
	 * Create the LookupField.
	 */
	public LookupField() {
		initFrame();
	}
	
	/**
	 * Setup Panel
	 */
	private void initFrame(){
		setBounds(0, 0, 220, 20);
		setLayout(new BorderLayout(0, 0));
		
		button = new JButton("");
		button.setIcon(new ImageIcon(LookupField.class.getResource("/net/gianrishandy/jbase/images/cari20.png")));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					showDataList();
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, e2);
				}
			}
		});
		add(button, BorderLayout.EAST);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				try {
					if(e.getKeyCode() == KeyEvent.VK_F2){
						showDataList();
					}
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, e2);
				}
			}
		});
		add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
	}
	
	private void showDataList() throws Exception{
		beforeShowDataList();
		dataList = new DataList();
		dataList.setDataTable(dataTable);
		dataList.addDataListListener(new DataListAdapter() {
			@Override
			public void actionPerformed(DataListEvent evt) {
				LookupEvent event = new LookupEvent(this);
				event.setDataRow(evt.getDataRow());
				
				lookupActionPerformed(event);
			}
		});
		dataList.showDataList();
	}
	
	/**
	 * Request focus for LookupField
	 */
	public void requestFocus(){
		textField.requestFocus();
	}
	
	/**
	 * Get LookupField text
	 * 
	 * @return String
	 */
	public String getText(){
		return textField.getText();
	}
	
	/**
	 * Set LookupField text
	 * 
	 * @param text
	 */
	public void setText(String text){
		textField.setText(text);
	}
	
	/**
	 * Set enabled for LookupField
	 * @param b-boolean(true = enable and false = disable)
	 */
	public void setEnabled(boolean b){
		textField.setEnabled(b);
		button.setEnabled(b);
	}
	
	/** 
	 * Check LookupField enable
	 * @return boolean(true = enable and false = disable)
	 */
	public boolean isEnabled(){
		return textField.isEnabled();
	}
	
	/**
	 * Set Editable for LookupField
	 * @param b-boolean (true = editable and false = non-editable)
	 */
	public void setEditable(boolean b){
		textField.setEditable(b);
		button.setEnabled(b);
	}
	
	/**
	 * Check LookupField Editable
	 * 
	 * @return boolean (true = editable and false = non-editable)
	 */
	public boolean isEditable(){
		return textField.isEditable();
	}
	
	/**
	 * Get LookupField DataTable
	 * 
	 * @return DataTable
	 */
	public DataTable getDataTable() {
		return dataTable;
	}
	
	/**
	 * Set DataTable For LookupField, Better use this on BeforeShowDataList();
	 * 
	 * @param dataTable - DataTable
	 */
	public void setDataTable(DataTable dataTable) {
		this.dataTable = dataTable;
	}
	
	public void addLookupListener(LookupAdapter listener){
		listenerList.add(LookupListener.class, listener);
	}
	
	public void removeLookupListener(LookupAdapter listener){
		listenerList.remove(LookupListener.class, listener);
	}
	
	private void lookupActionPerformed(LookupEvent evt){
		Object[] list = listenerList.getListenerList();
		for(int i=0;i<list.length;i+=2){
			if(list[i] == LookupListener.class){
				((LookupListener)list[i+1]).actionPerformed(evt);
			}
		}
	}
	
	private void beforeShowDataList(){
		Object[] list = listenerList.getListenerList();
		for(int i=0;i<list.length;i+=2){
			if(list[i] == LookupListener.class){
				((LookupListener)list[i+1]).beforeShowDataList();
			}
		}
	}
}
